/**
 *
 */
package com.naiterui.ehp.bs.im.service.message.handler;

import org.springframework.stereotype.Service;

import com.naiterui.ehp.bs.im.network.protocl.Replay;
import com.naiterui.ehp.bs.im.repository.mongo.ReplayDao;
import com.naiterui.ehp.bs.im.vo.ReplayVO;

@Service
public class ReplayHandler {

    /**
     * 关闭自动回复的操作
     */
    private static final int TRUN_OFF = 0;

    /**
     * 打开自动 回复操作
     */
    private static final int TRUN_ON = 1;

    /**
     *
     */
    private static final int FIND_REPLAY = 2;

    /**
     * 状态值 开启
     */
    private static final int OFF = 0;
    /**
     * 状体值关闭
     */
    private static final int ON = 1;

    private final ReplayDao replayDao;

    public ReplayHandler(ReplayDao replayDao) {
        this.replayDao = replayDao;
    }

    /**
     * 设置或获取自动回复
     */
    public ReplayVO setOrGetReplay(Replay replay, Integer operate) {
        ReplayVO replayVo = null;
        switch (operate) {
            case TRUN_OFF: {
                // 如果 是关闭操作
                replayDao.upsert(replay.getUserId(), replay.getContent(), OFF);
                break;
            }
            case TRUN_ON: {
                // 如果是开启操作
                replayDao.upsert(replay.getUserId(), replay.getContent(), ON);
                break;
            }
            case FIND_REPLAY: {
                // 查询操作
                Replay result = replayDao.findReplay(replay.getUserId());
                if (result != null) {
                    replayVo = new ReplayVO();
                    replayVo.setStatus(result.getStatus());
                    replayVo.setContent(result.getContent());
                }
            }
        }
        return replayVo;
    }

    /**
     * 获取当前自动 回复消息
     */
    public String findCurrReplay(Long userId) {
        Replay replay = replayDao.findReplay(userId);
        if (replay != null) {
            return replay.getContent();
        }
        return null;
    }

    /**
     * 当前用户 是否开启了 自动回复
     */
    public boolean isReplay(Long userId) {
        Replay replay = replayDao.findReplay(userId);
        return replay != null && replay.getStatus() == ON;
    }

}
